﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCRUM.Core.BusinessObjects
{
    /// <summary>
    /// Holds default column definition for all entities
    /// </summary>
    [Serializable()]
    public class ColumnDef
    {
        /// <summary>
        /// constant for type project
        /// </summary>
        public const string project = "Project";

        /// <summary>
        /// constant for type release
        /// </summary>
        public const string release = "Release";

        /// <summary>
        /// constant for type sprint
        /// </summary>
        public const string sprint = "Sprint";

        /// <summary>
        /// constant for type user story
        /// </summary>
        public const string userStory = "UserStory";

        /// <summary>
        /// constant for type bli
        /// </summary>
        public const string bli = "Bli";
        
        /// <summary>
        /// constant for type task
        /// </summary>
        public const string task = "Task";

        /// <summary>
        /// constant for type dailyScrum
        /// </summary>
        public const string dailyScrum = "DailyScrum";
        /// <summary>
        /// Columns to display and their visibility
        /// </summary>
        public Dictionary<String, bool> Columns{ get; set;}

        /// <summary>
        /// Type of column description, one from const defined in this class
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type of default column definition</param>
        /// <param name="generateDefault">if default columns should be generated</param>
        public ColumnDef(string type)
        {
            this.Type = type;
            this.Columns = new Dictionary<String, bool>();
                switch (type)
                {
                    case project:
                        {
                            AddColumn("Name", true);
                            AddColumn("Customer", true);
                            AddColumn("StartDate", false);
                            AddColumn("State", true);
                            AddColumn("Progress", false);
                            AddColumn("Estimation", true);
                            break;
                        }
                    case release:
                        {
                            AddColumn("Name", true);
                            AddColumn("Version", false);
                            AddColumn("Progress", false);
                            AddColumn("Estimation", true);
                            break;
                        }
                    case sprint:
                        {
                            AddColumn("Name", true);
                            AddColumn("Description", false);
                            AddColumn("State", true);
                            AddColumn("FromDate", false);
                            AddColumn("ToDate", false);
                            AddColumn("Progress", false);
                            AddColumn("Estimation", true);
                            break;
                        }
                    case userStory:
                        {
                            AddColumn("Name", true);
                            AddColumn("Description", false);
                            AddColumn("Estimation", true);
                            AddColumn("Progress", false);
                            AddColumn("State", true);
                            break;
                        }
		    case bli:
                        {
                            AddColumn("Name", true);
                            AddColumn("Description", true);
                            AddColumn("Estimation", true);
                            AddColumn("Progress", false);
                            break;
                        }
                    case task:
                        {
                            AddColumn("Name", true);
                            AddColumn("Description", true);
                            AddColumn("Estimation", true);
                            AddColumn("Progress", false);
                            AddColumn("State", true);
                            break;
                        }
                    case dailyScrum:
                        { 
                            AddColumn("Date", true);
                            AddColumn("Task", true);
                            AddColumn("Did", true);
                            AddColumn("ToDo", true);
                            AddColumn("Problems", true);
                            AddColumn("Comments", true);
                            AddColumn("StoryPoints", true);
                            return;
                        }

                }
        }


        /// <summary>
        /// Adds new column
        /// </summary>
        /// <param name="key">Column name</param>
        /// <param name="value">visibility</param>
        public void AddColumn(string key, bool value)
        {
            this.Columns.Add(key, value);
        }

        public List<string> GetColumns()
        {
            return new List<string>(Columns.Keys.ToArray());
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type of column definition</param>
        /// <param name="text">Text for column definition</param>
        public ColumnDef(string type, string text) :this(type) 
        {
            parseColumnDef(text);
        }

        /// <summary>
        /// ColumnDef is stored as string in form columnName:visible;columnName:visible;
        /// Thid method parses setting and produce Columndef object
        /// </summary>
        /// <param name="cd">column definition with predefined type</param>
        /// <param name="textToParse">value from settings</param>
        private void parseColumnDef(string textToParse)
        {
            string[] allColumns = textToParse.Split(';');
            Dictionary<string, bool> result = new Dictionary<string, bool>();

            foreach (string col in allColumns)
            {
                string[] value = col.Split(':');
                if (value[0] != "")
                {
                    bool visible = value[1].StartsWith("t") || value[1].StartsWith("T");
                    if (!value[0].StartsWith("Enabled"))
                    {
                        result.Add(value[0], visible);
                    }
                }
            }

            if (this.Columns.Count == result.Count)
            {
                this.Columns = result;
            }
            else 
            {
                throw new Exception("Column def wrong!");
            }
        }
    }
}
